/*
 * Copyright (C) 2004, Velocityme Partnership
 * GroupValueActionForm.java
 *
 * Created on 09 October 2004, 12:21
 */

package com.velocityme.www.actionforms.nodeactionforms;

import com.velocityme.entity.Group;
import com.velocityme.entity.User;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.UserLocal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author  Robert
 */
public class GroupActionForm extends NodeActionForm {
    
    private Long[] m_unassignedUserIds;
    private Long[] m_assignedUserIds;

    public void extractGroup(KeyLocal key, Group group) {
        extractNode(group);
        group.clearUsers();
        UserLocal userLocal = lookupUserBean();
        if (m_assignedUserIds != null) {
            for (Long id: m_assignedUserIds) {
                User user = userLocal.findUser(key, id);
                group.addUser(user);
            }
        }
    }

    public Long[] getUnassignedUserIds() {
        return m_unassignedUserIds;
    }
    
    public void setUnassignedUserIds(Long[] unassignedUserIds) {
        m_unassignedUserIds = unassignedUserIds;
    }
    
    public Long[] getAssignedUserIds() {
        return m_assignedUserIds;
    }
    
    public void setAssignedUserIds(Long[] assignedUserIds) {
        m_assignedUserIds = assignedUserIds;
    }

    private UserLocal lookupUserBean() {
        try {
            Context c = new InitialContext();
            return (UserLocal) c.lookup("java:comp/env/UserBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
}
